package extension.game.room.handler;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import extension.game.room.Game;
import extension.game.room.Player;
import extension.game.room.GameResponse;
import extension.game.room.RoomExtension;
import log.Debug;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author daz
 */

public class BetMoneyHandler extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject params) {
        int ratioMoney = params.getInt(RoomFlag.BET_MONEY);
        Debug.d(user.getName() + "-bet money :" + ratioMoney);
        RoomExtension phomExtension = (RoomExtension) getParentExtension();
        Game game = phomExtension.getGame();
        Player[] listPlayers = game.getListPlayers();
        int seatPos = -1;
        for (Player p : listPlayers) {
            if (p.getName().equals(user.getName())) {
                seatPos = p.getSeatPos();
                break;
            }
        }
        GameResponse response = phomExtension.getResponse();
        if (!game.getIsPlay()) {
            if (seatPos == game.getTableHost()) {
                if (listPlayers[seatPos].getMoney() > game.getBetMin() * 5 * ratioMoney) {
                    game.setBetMoney(ratioMoney);
                    response.sendBetMoney(game.getBetMoney(), 1);
                    game.updateReadyState();
                } else {
                    response.sendBetMoney(game.getBetMoney(), 0, listPlayers[seatPos].getUser());
                }
            }
        }

    }
}
